/**
* @file        bsp.c
* @brief       This file provides all the basic firmware functions.
* @author      ChipSea MCU Group
* @version     1.0.0
* @date        2022.04.25
* @copyright   CHIPSEA TECHNOLOGIES (SHENZHEN) CORP.
* @note        
****************************************************************************************************
* @par Modify record:
* <tr><th>Date          <th>Version    <th>Author     <th>Description
* <tr><td>2022.04.25    <td>1.0.0      <td>ChipSea    <td>
* -# 
* </table>
*
****************************************************************************************************
*/


/***************************************************************************************************
 * @includes
***************************************************************************************************/
#include "bsp.h"
#include "include.h"



/***************************************************************************************************
 * @definitions
***************************************************************************************************/

/** @defgroup bsp
  * @brief    bsp driver modules
  * @{
  */

///@cond

/***************************************************************************************************
 * @variables
***************************************************************************************************/


/***************************************************************************************************
 * @function
***************************************************************************************************/
/**@brief       Init LED1 and LED2.
 *
 * @param[in]   None.
 *
 * @return      None.
 */
static void led_init(void)
{
    /* Enable the clock */
    __RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_GPIOB);
    
    gpio_mode_config(GPIOB,GPIO_PIN_15,GPIO_MODE_OUT_PP(GPIO_SPEED_HIGH));
    gpio_mode_config(GPIOB,GPIO_PIN_14,GPIO_MODE_OUT_PP(GPIO_SPEED_HIGH));
    
    __GPIO_PIN_RESET(GPIOB,GPIO_PIN_15);     //LED1
    __GPIO_PIN_RESET(GPIOB,GPIO_PIN_14);    //LED2
}

/**@brief       Toggle the LED.
 *
 * @param[in]   None.
 *
 * @return      None.
 */
void led1_toggle(void)
{
    GPIOB->DO ^= GPIO_PIN_15;
}

void led2_toggle(void)
{
    GPIOB->DO ^= GPIO_PIN_14;
}

/**@brief       Configure TIM3 and TIM4.
 *
 * @param[in]   None.
 *
 * @return      None.
 */
void tim3_config(uint16_t arr, uint16_t psc)
{
    tim_base_t ptr_time = {0};
    nvic_init_t ptr_nvic;
    
    __RCU_APB1_CLK_ENABLE(RCU_APB1_PERI_TIM3);
    
    /* Time base configuration */
    ptr_time.clk_div = TIM_CLK_DIV1;
    ptr_time.cnt_mode = TIM_CNT_MODE_UP;
    ptr_time.period = arr;
    ptr_time.pre_div = psc;
    ptr_time.repeate_count = 0;
    tim_base_init(TIM3, &ptr_time);
    
    __TIM_INTR_ENABLE(TIM3, TIM_INTR_UPDATE);
    
    /* Configure and enable TIM3 interrupt. */
    ptr_nvic.nvic_irqchannel = IRQn_TIM3;
    ptr_nvic.nvic_irq_enable = ENABLE;
    ptr_nvic.nvic_irq_pre_priority = 5;
    ptr_nvic.nvic_irq_sub_priority = 0;
    nvic_init(&ptr_nvic);
    
    __TIM_ENABLE(TIM3);
}


/**@brief       KEY init.
 *
 * @param[in]   None.
 *
 * @return      None.
 */
void key_init(void)
{
    __RCU_APB2_CLK_ENABLE(RCU_APB2_PERI_GPIOC);
    /* PC13 -- button */
    gpio_mode_config(GPIOC, GPIO_PIN_13, GPIO_MODE_IN_PD);
}



/**@brief       Initialize chip periphral.
 *
 * @param[in]   None.
 *
 * @return      None.
 */
void bsp_init(void)
{
    usart_config();
    led_init();
    Key1_GPIO_Config();
    KeyCreate(&Key1,GetPinStateOfKey1);
}

///@endcond

/**
  * @}
  */

